package com.das.mechanic_base.mvp.presenter.create;

import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.bean.create.MapReceiveStatusBean;
import com.das.mechanic_base.bean.create.ServiceInfoBean;
import com.das.mechanic_base.bean.main.CarPicAndColorBean;
import com.das.mechanic_base.bean.main.CarSelectBean;
import com.das.mechanic_base.bean.main.DetectionNewBean;
import com.das.mechanic_base.bean.main.HomeMainCarBean;
import com.das.mechanic_base.bean.main.UpadateReceBean;
import com.das.mechanic_base.bean.main.UpdateReceiveCarBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.mvp.contract.create.X3ServiceInfoContract;
import com.das.mechanic_base.utils.X3StringUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class X3ServiceInfoPresenter extends X3BasePresenter<X3ServiceInfoContract.View> implements X3ServiceInfoContract.Presenter {

    private UpdateReceiveCarBean carBean;

    @Override
    public void getReceiveForMechanic(long receiveId) {
        NetWorkHttp.getApi().getReceiveBaseForMechanic(receiveId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<ServiceInfoBean>() {
                    @Override
                    protected void onDone(ServiceInfoBean infoBean) {
                        mView.getReceiveSuccess(infoBean);
                    }

                    @Override
                    protected void NoNetCallBack() {
                        super.NoNetCallBack();
                        mView.getReceiveSuccess(null);
                    }

                    @Override
                    protected void showError(String error) {
                        mView.getReceiveSuccess(null);
                    }
                });

    }

    @Override
    public void getReceiveStatusList(long receiveBaseId) {

        NetWorkHttp.getApi().mapReceiveListStatus(receiveBaseId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<MapReceiveStatusBean>() {
                    @Override
                    protected void onDone(MapReceiveStatusBean statusBean) {
                        mView.getReceiveStatusListSuccess(statusBean);
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void getReceiveBase(long receiveId) {
        NetWorkHttp.getApi().getReceiveBase(receiveId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<HomeMainCarBean>() {
                    @Override
                    protected void onDone(HomeMainCarBean mainCarBean) {
                        mView.getReceiveBase(mainCarBean);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void requestVin(File carVin) {
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), carVin);
            String  filename = URLEncoder.encode(carVin.getName(), "UTF-8");
            MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, requestBody);

            NetWorkHttp.getApi().vinRecognition(body)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<String>() {
                        @Override
                        protected void onDone(String vin) {
                            mView.getVin(vin);
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {

                        }
                    });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateReceiveCar(CarSelectBean carSelectBean, String carNum) {
        if (carBean == null) {
            carBean = new UpdateReceiveCarBean();
        }
        //参数 车型
        if (!X3StringUtils.isEmpty(carSelectBean.getCarStyleName())) {
            carBean.setCarSeriesId(carSelectBean.getCarTypeId());
            String carStyleName = carSelectBean.getCarStyleName();
            String[] split = carStyleName.split("\\(");
            carBean.setCarSeriesName(split[0]);
        }
        //参数 车辆品牌
        if (!X3StringUtils.isEmpty(carSelectBean.getCarBrand())) {
            carBean.setCarBrandId(carSelectBean.getCarBrandId());
            carBean.setCarBrandName(carSelectBean.getCarBrand());
        }
        //参数年款
        if (!X3StringUtils.isEmpty(carSelectBean.getCarYear())) {
            carBean.setCarStyleId(carSelectBean.getCarStyleId());
            carBean.setCarStyleName(carSelectBean.getCarYear());
        }
        carBean.setCarVin(carSelectBean.getVin() + "");

        carBean.setColorId(carSelectBean.getColorId());

        carBean.setCarPicTempletId(carSelectBean.getCarPicTempletId());

        if (!X3StringUtils.isEmpty(carSelectBean.getCarMiles())) {
            carBean.setEstimateCurrentMiles(carSelectBean.getCarMiles());
        }
        if (!X3StringUtils.isEmpty(carSelectBean.getPerMiles())) {
            carBean.setPerMiles(carSelectBean.getPerMiles());
        }
        carBean.setCarProductionDate(carSelectBean.getCarProductionDate());

        if (!X3StringUtils.isEmpty(carNum)) {
            carBean.setCarNum(carNum);
        }

        if (!X3StringUtils.isEmpty(carSelectBean.getCarOwnerName())) {
            carBean.setCarOwnerName(carSelectBean.getCarOwnerName());
        }
        if (!X3StringUtils.isEmpty(carSelectBean.getGender())) {
            carBean.setGender(carSelectBean.getGender());
        }

        if (!X3StringUtils.isEmpty(carSelectBean.getMobile())) {
            carBean.setMobile(carSelectBean.getMobile());
        }

        if (carSelectBean.getCarTypeId() != 0) {
            carBean.setCarTypeId(carSelectBean.getCarTypeId());
        }
        if (!X3StringUtils.isEmpty(carSelectBean.getLastName())) {
            carBean.setLastName(carSelectBean.getLastName());
        }

        carBean.setChatroomBaseIdList(new ArrayList<>());

        carBean.setId(carSelectBean.getId());

        NetWorkHttp.getApi().updateReceiveCar(carBean)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<UpadateReceBean>() {
                    @Override
                    protected void onDone(UpadateReceBean upadateReceBean) {
                        mView.updateCarTypeSuccess();
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {
                        mView.updateCarTypeFail();
                    }
                });
    }

    @Override
    public void uploadCarPic(long carId, String imgPath, String carNum) {
        File imgFile = new File(imgPath);
        if (!imgFile.exists()) {
            return;
        }
        try {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), imgFile);
            String  filename = URLEncoder.encode(imgFile.getName(), "UTF-8");
            MultipartBody.Part fileBody = MultipartBody.Part.createFormData("multipartFile", filename, requestBody);

            NetWorkHttp.getApi().createCarPic(carId,carNum,fileBody)
                    .compose(RxSchedulersHelper.defaultComposeRequest())
                    .compose(mView.bindToLife())
                    .subscribe(new HttpCallBack<CarPicAndColorBean>() {
                        @Override
                        protected void onDone(CarPicAndColorBean colorBean) {
                            mView.uploadCarPicSuccess(colorBean);
                        }

                        @Override
                        protected String LoadingMessage() {
                            return null;
                        }

                        @Override
                        protected void showError(String error) {
                            mView.uploadCarPicSuccess(null);
                        }
                    });
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void closeReceiveBase(long receiveBaseId) {
        NetWorkHttp.getApi().closeReceiveBase(receiveBaseId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        mView.closeReceiveBaseSuccess();
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void getDetectionFirstInfo(long receiveBaseId) {
        NetWorkHttp.getApi().getDetectionFirstInfo(receiveBaseId,true)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<List<DetectionNewBean>>() {
                    @Override
                    protected void onDone(List<DetectionNewBean> lists) {
                        mView.getDetectionFirstInfoSuccess(lists);
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }

    @Override
    public void saveDetectionFirstInfo(List<DetectionNewBean> mList,long receiveId) {
        NetWorkHttp.getApi().saveDetectionFirstInfo(mList,receiveId)
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .compose(mView.bindToLife())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        mView.saveDetectionFirstInfoSuccess();
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }
}
